package com.chujian.Activities;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;

import com.chujian.Global.Global;
import com.chujian.Threads.HttpClientGetThread;
import com.chujian.Utils.Constan;
import com.chujian.Utils.JsonUtil;
import com.chujian.orderingsystem.R;

public class FoodCategaryActivity extends Activity implements OnClickListener,
	   OnItemClickListener {
	private ListView mListView;
	private Button mBtnAddCategary;
	private ImageButton mIbtnBack;
	private List<String> mList;
	private Handler mHandler;
	private ArrayAdapter<String >mAdapter;
	private boolean addCategaryFlag;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.food_categary);
		initView();
		mHandler=new Handler(){
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				String str=(String)msg.obj;
				if(!str.equals("")){
					
					{
					mList=JsonUtil.StringArrayFromJson(str);
					mAdapter=new ArrayAdapter<String>(FoodCategaryActivity.this, android.R.layout.simple_expandable_list_item_1, mList);
					mListView.setAdapter(mAdapter);
				}
			}
			}
		};
		String url=Constan.BASE_URL+"FoodCategaryServlet";
		new HttpClientGetThread(url,mHandler).start();
		
		
	}
	

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if("".equals(Global.categaryNewAdd)||Global.categaryNewAdd==null){
			
		}else if(addCategaryFlag){
			mList.add(Global.categaryNewAdd);
			Global.categaryNewAdd="";
			addCategaryFlag=false;
			mAdapter.notifyDataSetChanged();
		}
	}


	private void initView() {
		// 初始化
		mListView=(ListView) findViewById(R.id.lv_food_categary);
		mBtnAddCategary=(Button) findViewById(R.id.btn_food_cg_add);
		mIbtnBack=(ImageButton) findViewById(R.id.ib_header_back);
		mBtnAddCategary.setOnClickListener(this);
		mIbtnBack.setOnClickListener(this);
		mListView.setOnItemClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_food_cg_add:
//			添加新的类别
//			添加类别时同时要求添加菜品才能完成类别的创建
			addCategaryFlag=true;
			Intent intent=new Intent(FoodCategaryActivity.this, AddCategaryDialog.class);
			startActivity(intent);
			break;
		case R.id.ib_header_back:
			FoodCategaryActivity.this.finish();
			break;
		default:
			break;
		}
	}


	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
		String categary=mList.get(position);
		Toast.makeText(FoodCategaryActivity.this, categary, Toast.LENGTH_SHORT).show();
		Intent intent=new Intent(FoodCategaryActivity.this, FoodListActivity.class);
		intent.putExtra("foodCategary", categary);
		startActivity(intent);
	}
}
